package util.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Date;

public class UDPServer {

	private final static int PORT = 13;

	public static void main(String[] args) {
		try (DatagramSocket socket = new DatagramSocket(PORT)) {
			while (true) {
				try {
					byte[] requestData = new byte[1024];
					DatagramPacket request = new DatagramPacket(requestData, requestData.length);
					socket.receive(request);
					String info = new String(requestData,0,request.getLength());
					Thread.sleep(Integer.parseInt(info));
					String daytime = new Date().toString();
					byte[] data = daytime.getBytes("UTF-8");
					DatagramPacket response = new DatagramPacket(data, data.length, request.getAddress(),
							request.getPort());
					socket.send(response);
					System.out.println(daytime + " " + request.getAddress() + " " + "客户端说：" + info);
				} catch (IOException e) {
					e.printStackTrace();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
